﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TestMySqlConnetPoll;
using System.Threading;
using DevExpress.XtraBars.Alerter;

namespace View.Journal
{
    /// <summary>
    /// 系统工作日志
    /// </summary>
    public partial class JournalLogForm : DevExpress.XtraEditors.XtraForm
    {
        //系统工作日志
        private SysLogTable sysLog = new SysLogTable();
        private DataTable bindTable = new DataTable();
        private AlertControl alart = new AlertControl();

        public JournalLogForm()
        {
            InitializeComponent();

            //初始化数据绑定表
            bindTable.Columns.Add("text");
            bindTable.Columns.Add("time");

            //设置日志类型初始选择
            comboBox_type.SelectedIndex = 0;

            if (!ReadValue())
            {
                if (alart.AlertFormList.Count > 0)
                {
                    alart.AlertFormList[0].Close();
                }
                alart.Show(this, "注意", "没有查询到数据");
            }
        }



        private void ComboBox_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Text = (string)comboBox_type.SelectedItem;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnS_Click(object sender, EventArgs e)
        {
            if(!ReadValue())
            {
                if (alart.AlertFormList.Count > 0)
                {
                    alart.AlertFormList[0].Close();
                }
                alart.Show(this, "没有查询到数据", "");
            }



            //if (dateEdit_S.EditValue.Equals(dateEdit_E))    //开始与结束日期相同
            //{
            //    objDate date = new objDate();
            //    date.year = ((DateTime)dateEdit_S.EditValue).Year;
            //    date.month = (byte)((DateTime)dateEdit_S.EditValue).Month;
            //    date.day = (byte)((DateTime)dateEdit_S.EditValue).Day;
            //    hasValue = ReadValue(date, (string)comboBox_type.SelectedItem);

            //    if (!hasValue)
            //        return;
            //    foreach (SysLogTable.objSystemLog log in sysLog.ValueList[(string)comboBox_type.SelectedItem])
            //    {
            //        bindTable.Rows.Add(new object[] { log.logText, log.DtTime });
            //    }
            //}
            //else                //开始于结束日期不一致
            //{
            //    TimeSpan dtTemp = ((DateTime)dateEdit_E.EditValue).Subtract((DateTime)dateEdit_S.EditValue);
            //    DateTime timeTemp = (DateTime)dateEdit_S.EditValue;

            //    for (int i = 0; i < (int)dtTemp.TotalDays; i++)
            //    {
            //        objDate date = new objDate();
            //        date.year = timeTemp.AddDays(i).Year;
            //        date.month = (byte)timeTemp.AddDays(i).Month;
            //        date.day = (byte)timeTemp.AddDays(i).Day;
            //        hasValue = ReadValue(date, (string)comboBox_type.SelectedItem);
            //    }

            //    if (!hasValue)
            //        return;

            //    foreach (SysLogTable.objSystemLog log in sysLog.ValueList[(string)comboBox_type.SelectedItem])
            //    {
            //        bindTable.Rows.Add(new object[] { log.logText, log.DtTime });
            //    }
            //}

           
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="date"></param>
        /// <param name="strLogType"></param>
        private bool ReadValue()
        {
            bindTable.Rows.Clear();

            objDate date = new objDate();
            date.year = dateEdit_S.Value.Year;
            date.month = (byte)dateEdit_S.Value.Month;
            date.day = (byte)dateEdit_S.Value.Day;

            sysLog.ReadValueList(date, (string)comboBox_type.SelectedItem);

            while (!sysLog.ReadyValueList)
            {
                if (sysLog.ErrorFlag != 0)
                {
                    return false;
                }

                Thread.Sleep(20);
            }

            if (!sysLog.ValueList.ContainsKey((string)comboBox_type.SelectedItem))
                return false;

            foreach (SysLogTable.objSystemLog log in sysLog.ValueList[(string)comboBox_type.SelectedItem])
            {
                bindTable.Rows.Add(new object[] { log.logText, log.DtTime });
            }

            if (bindTable.Rows.Count == 0)
                return false;

            this.grid_main.DataSource = bindTable;

            return true;
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnP_Click(object sender, EventArgs e)
        {
            grid_main.ShowRibbonPrintPreview();
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnE_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = "Excel(*.xlsx)|*.xlsx";
            sf.FileName = (string)comboBox_type.SelectedItem;

            if (sf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                grid_main.ExportToXlsx(sf.FileName);
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnC_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void DateEdit_S_EditValueChanged(object sender, EventArgs e)
        //{
        //    if (((DateTime)dateEdit_S.EditValue).CompareTo((DateTime)dateEdit_E.EditValue) > 0)
        //    {
        //        dateEdit_S.EditValue = dateEdit_E.EditValue;
        //    }
        //}

        //private void DateEdit_E_EditValueChanged(object sender, EventArgs e)
        //{
        //    if (((DateTime)dateEdit_S.EditValue).CompareTo((DateTime)dateEdit_E.EditValue) > 0)
        //    {
        //        dateEdit_E.EditValue = dateEdit_S.EditValue;
        //    }
        //}
    }
}